#!/usr/bin/env python
# -*- coding: utf-8 -*-

# thirdpart modules
from PyQt5.QtCore import (
    QModelIndex,
    QSize,
    Qt,
    pyqtSignal as QSignal,
)

from PyQt5.QtGui import (
    QCursor,
    QIcon,
    QStandardItem,
    QStandardItemModel,
)

from PyQt5.QtWidgets import (
    QAbstractItemView,
    QAction,
    QDockWidget,
    QMainWindow,
    QMenu,
    QToolBar,
    QTreeView,
)

class TabDockWindow(QDockWidget):
    viewItem = QSignal(QModelIndex)

    def __init__(self, title='Tables', parent=None):
        super().__init__(title, parent)
        self.tableNames = []
        if parent and hasattr(parent, 'tableNames'):
            self.tableNames = parent.tableNames

        self.dataModel()
        self.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
        self.toggleViewAction().setIcon(QIcon(':/Resources/Images/table.png'))
        parent and hasattr(parent, 'viewMenu')    and parent.viewMenu.addAction(self.toggleViewAction())
        parent and hasattr(parent, 'fileToolBar') and parent.fileToolBar.addAction(self.toggleViewAction())

        self.treeView = QTreeView(self)
        self.treeView.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.treeView.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.treeView.setContextMenuPolicy(Qt.CustomContextMenu)
        self.treeView.customContextMenuRequested.connect(self.dockTabContextMenuResponse)
        self.treeView.doubleClicked.connect(self.onDoubleClicked)
        self.treeView.header().hide()
        self.treeView.setModel(self.standardItemModel)
        self.treeView.expandAll()

        self.innerMain = QMainWindow(self)
        self.innerMain.setWindowFlags(Qt.Widget)
        self.toolBar = QToolBar(self.innerMain)
        self.toolBar.setIconSize(QSize(16, 16))
        self.toolBar.addAction(self.toggleViewAction())
        self.innerMain.addToolBar(self.toolBar)
        self.innerMain.setCentralWidget(self.treeView)
        self.setWidget(self.innerMain)

    def dataModel(self):
        self.standardItemModel = QStandardItemModel(self)
        self.standardItemModel.setHorizontalHeaderLabels(['Tables'])
        root = QStandardItem(QIcon(':/Resources/Images/book_angle.png'), 'Tables')
        items = map(lambda x: QStandardItem(QIcon(':/Resources/Images/table.png'), x), self.tableNames)
        root.appendRows(items)
        self.standardItemModel.invisibleRootItem().appendRow(root)

    def dockTabContextMenuResponse(self, pos):
        isSelected = not not self.treeView.selectedIndexes()
        self.viewAct = QAction('&View', self,
            statusTip = 'View tables',
            enabled = isSelected,
            triggered = self.viewTables)

        font = self.viewAct.font()
        font.setBold(True)
        self.viewAct.setFont(font)

        self.editAct = QAction('&Edit', self,
            statusTip = 'Edit tables',
            enabled = isSelected,
            triggered = self.viewTables)

        self.contextMenu = QMenu(self)
        self.contextMenu.addAction(self.viewAct)
        self.contextMenu.addAction(self.editAct)
        self.contextMenu.exec_(QCursor.pos())

    def viewTables(self):
        indexes = self.treeView.selectedIndexes()
        for index in indexes:
            self.viewItem.emit(index)

    def onDoubleClicked(self, index):
        self.viewItem.emit(index)

if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication

    app = QApplication(sys.argv)
    main = QMainWindow()
    main.dockTab = TabDockWindow(parent=main)
    main.addDockWidget(Qt.LeftDockWidgetArea, main.dockTab)
    main.show()
    sys.exit(app.exec_())
